<!DOCTYPE HTML>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<!-- 客户升级页面 -->
<body class="gray-bg"style="background-color: white;">
   	<br>
	<div class="select-list">
		<ul >
			<li>
				&nbsp;&nbsp;&nbsp;用户电话：&nbsp;&nbsp;<input id="pHONE" type="text" name="pHONE"  placeholder="请输入电话号码"/>
				&nbsp;&nbsp;<a class="btn btn-primary btn-rounded btn-sm" onclick="queryMerchan()"><i class="fa fa-search"></i>&nbsp;搜索</a>
			</li>
		</ul>
	</div>
			<!-- <table style="border: 0px;">
				<tr height="25"></tr>
				<tr >
					<td width="10%"></td>
					<td >
						
					</td>
				</tr>
				<tr height="20px"></tr>
			</table>		 -->
			<!-- 用户信息 -->
	<br><br>
	<table style="width: 100%">
		<tr>
			<td width="7%"></td>
			<td>
				<div class="form-group" id="merchanInfo" style="display: none">
					<!-- 隐藏保存用户id -->
					<input id="merchantId" style="display: none" type="text">
					<table>
						<tr height="45">
							<td style="font-weight: bold;">用户编号：</td>
							<td><span id="merchantNo"></span></td>
						</tr>
						<tr height="45">
							<td style="font-weight: bold;">用户名称：</td>
							<td><span id="merchantCnName"></span></td>
						</tr>
						<tr height="45">
							<td style="font-weight: bold;">用户电话：</td>
							<td><span id="phone"></span></td>
						</tr>
						<tr height="45">
							<td style="font-weight: bold;">用户账户总金额：</td>
							<td><span id="totalMoney"></span>元</td>
						</tr>
						<tr height="45">
							<td style="font-weight: bold;">用户等级：</td>
							<td><span id="level"></span></td>
						</tr>
						<tr height="45">
							<td  style="font-weight: bold;">您的剩余升级码数量：</td>
							<td><span id="fastCount"></span>个</td>
						</tr>
						<tr height="45">
							<td  style="font-weight: bold;">请选择配置的升级码个数：</td>
							<td>
								<select id="codeCountSelect">
									<option value="25" selected="selected">25&nbsp;&nbsp;&nbsp;</option>
									<option value="15">15&nbsp;&nbsp;&nbsp;</option>
								</select>
							</td>
						</tr>
						<tr></tr>
					</table>
					<!-- <div class="form-control-static"  >
								</div>
								<label class="col-sm-7 control-label"></label>
								<div class="form-control-static" id="merchantCnName" >
								</div>
								<label class="col-sm-7 control-label">用户电话：</label>
								<div class="form-control-static" id="phone" >
								</div>
								<label class="col-sm-7 control-label">用户账户总金额：</label>
								<div  id ="totalMoney" class="form-control-static">
								</div>
								<label class="col-sm-7 control-label">用户等级：</label>
								<div  id ="level" class="form-control-static">
								</div>
								<label class="col-sm-7 control-label">用户升级码数量：</label>
								<div  id ="upgradeCodeNum" class="form-control-static">
								</div>
								<label class="col-sm-7 control-label">您的剩余升级码数量：</label>
								<div  class="form-control-static">
									<span  id ="fastCount" ></span>
								</div> 
								<label class="col-sm-7 control-label">
									<font >
											用户关系：
									</font>
								</label>
								<div class="form-control-static" >
									<font id="showRelation" >
											
									</font>
								</div>
								-->
					<p>
						<a class="btn btn-primary btn-rounded btn-lg"
							onclick="liftMerchant()">升级为经纪人</a>
					</p>
				</div>

				<p style="color: red; font-size: 16px">升级注意事项：</p>
				<p style="color: red; font-size: 14px">1.升级的用户必须是您的直推用户或者是您自己</p>
				<p style="color: red; font-size: 14px">
					2.该操作会扣掉您的升级码,并给用户加上对应个数的升级码
				</p>
				<p style="color: red; font-size: 14px">3.如果您的剩余升级码不足请联系您的上级</p>
				<p style="color: red; font-size: 14px">4.该操作一旦执行成功不能撤回</p>
			</td>
		</tr>
		<tr></tr>
	</table>
	<div th:include="include :: footer"></div>
    <script th:inline="javascript">
					var prefix = ctx + "system/hatchetMerchantForDLS";
					//通过手机号查询代理商的用户
					function queryMerchan() {
						var pHONE = $.trim($("#pHONE").val());
						/* var pattern = /^1[34578]\d{9}$/;
						if (!pattern.test(pHONE)) {
							$.modal.alertError("手机号输入有误");
							$("#pHONE").val("");
						return;
						} */
						$.ajax({
							url : prefix + "/queryMerchan?" + "pHONE=" + pHONE, //请求后台
							dataType : "json",
							type : "GET",
							async : true,
							success : function(re) {
								if (re.status == "FAIL") {
									$.modal.alertError(re.msg);
								} else {
									var merchan = re.hatchetMerchant;
									$("#merchantId").val(merchan.id);
									$("#merchantNo").text(merchan.merchantNo);
									$("#merchantCnName").text(
											merchan.merchantCnName);
									$("#phone").text(merchan.phone);
									var level = merchan.level;
									$("#level").text(getLevelName(level));
									$("#totalMoney").text(merchan.totalMoney);
									$("#upgradeCodeNum").text(
											merchan.upgradeCodeNum);
									//代理商剩余升级码数量
									$("#fastCount").text(re.fastCount);
									/* //显示需要扣除的升级码的数量
									$("#deductCount").text(re.codeCount); */
									//显示用户与代理商关系
									showRelation(merchan.id);
									$("#merchanInfo").show();
								}
							}
						});
					}

					//升级经纪人
					function liftMerchant() {
						var level = $("#level").text();
						if (level == "经纪人") {
							$.modal.alertError("该用户已经是经纪人无需升级");
							return;
						}
						var codeCount = $("#codeCountSelect").val();
						$.modal.confirm("您确定要将" + $("#merchantCnName").text()
								+ "升级成为经纪人？该操作会扣除您的" +codeCount
								+ "个升级码，操作成功后不可撤回", function() {
							var iD = $("#merchantId").val();
							$.ajax({
								url : prefix + "/liftMerchant",
								dataType : "json",
								type : "POST",
								data: {
									"iD":iD,
									"codeCount":codeCount
								},
								success : function(re) {
									if (re.status == "SUCCESS") {
										$.modal.msgSuccess(re.msg);
										//回显刷新用户信息
										$("#level")
												.text(getLevelName(re.level));
										$("#upgradeCodeNum").text(
												re.upgradeCodeNum);
										//代理商剩余升级码数量
										$("#fastCount").text(re.fastCount);
									} else {
										$.modal.msgError(re.msg);
									}
								}
							});
						});
					}

					function getLevelName(level) {
						if (level == "1") {
							return "普通用户";
						} else if (level == "2") {
							return "黄金用户";
						} else if (level == "3") {
							return "经纪人";
						}
					}
					//显示当前的用户与代理商中间的关系
					function showRelation(id) {
						$.ajax({
							url : ctx
									+ "system/hatchetMerchantForDLS/showRelation",
							dataType : "json",
							type : "GET",
							async : true,
							data : "id=" + id,
							success : function(re) {
								var str = "";
								//遍历代理商
								$.each(re, function(i, item) {
									str = str + item.type + " : "
											+ item.agentName + " | ";
								});
								var agent = re[0];
								var index = str.lastIndexOf(" | ")
								str = str.substring(0, index);
								$("#showRelation").text(str);
							}
						});
					}

					$(function() {
						//从配置表获取升级经纪人所需的升级码数量
						$.ajax({
							url : ctx
									+ "system/hatchetMerchantForDLS/getUpgradeCodeCountForJJR",
							dataType : "json",
							type : "GET",
							async : true,
							success : function(re) {
								$("#deductCount").text(re);
							}
						});
					})
				</script>
</body>
</html>